package pl.elka.simulator.gui.swt.painting;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;

import pl.elka.simulator.core.IPaintable;
import pl.elka.simulator.core.Pair;
import pl.elka.simulator.core.Position;
import pl.elka.simulator.core.experiment.NodePath;
import pl.elka.simulator.core.mobilityManager.impl.nodes.LeaderNode;
import pl.elka.simulator.gui.swt.PositionConverter;

public class PathPainter implements IElementPainter {

	private Color paintingColor = null;
	private Color leaderPaintingColor = null;
	
	@Override
	public void paint(IPaintable paintable,PaintEvent e) {
		NodePath n = (NodePath) paintable;
		if(n.getPath().size()<=1)
			return;
		
		GC gc = e.gc;
		if(paintingColor==null){
			paintingColor = new Color(e.display, 105, 21, 189);
		}
		if(leaderPaintingColor==null){
			leaderPaintingColor = new Color(e.display, 200, 0, 0);
		}
		if(n.getParent()!=null && n.getParent() instanceof LeaderNode){
			gc.setForeground(leaderPaintingColor);
		}else{
			gc.setForeground(paintingColor);
		}
				
		for(int i=1;i<n.getPath().size();++i){
			Position prevPosition = n.getPath().get(i-1);
			Position curPosition = n.getPath().get(i);
			
			Pair<Integer, Integer> prevPair = PositionConverter.convertFromPosition(prevPosition);
			Pair<Integer, Integer> curPair = PositionConverter.convertFromPosition(curPosition);
			
			gc.drawLine(prevPair.left, prevPair.right, curPair.left, curPair.right);
			
		}
							
	}
}
